<template>
    <div>
        <div class="operation">
            <a class="btn btn-theme" @click="showAddModal">添加部门</a>
            <a class="btn btn-theme" @click="showModifyModal">修改部门</a>
            <a class="btn btn-theme" @click="showDeleteModal">删除部门</a>
        </div>
        <table class="table">

        </table>
        <div class="modal fade" id="dept_add_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                        <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                        <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title" id="exampleModalLabel">添加部门</h4>
                                        </div>
                                        <div class="modal-body">
                                                <form>
                                                        <div class="form-group">
                                                                <label for="dept_add_name" class="control-label">部门名称：</label>
                                                                <input type="text" class="form-control" id="dept_add_name">
                                                        </div>
                                                        <div class="form-group">
                                                                <label for="dept_add_parent" class="control-label">上级部门：</label>
                                                                <input type="text" class="form-control" id="dept_add_parent">
                                                        </div>


                                                </form>
                                        </div>
                                        <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                                <button type="button" class="btn btn-primary" @click="addDept" id="dept_add_submit">提交</button>
                                        </div>
                                </div>
                        </div>
                </div>

                <div class="modal fade" id="dept_modify_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                        <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                        <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title" id="exampleModalLabel">修改部门</h4>
                                        </div>
                                        <div class="modal-body">
                                                <form>
                                                        <div class="form-group">
                                                                <label for="dept_modify_name" class="control-label">部门名称：</label>
                                                                <input type="text" class="form-control" id="dept_modify_name">
                                                        </div>
                                                        <div class="form-group">
                                                                <label for="dept_modify_parent" class="control-label">上级部门：</label>
                                                                <input type="text" class="form-control" id="dept_modify_parent">
                                                        </div>

                                                </form>
                                        </div>
                                        <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                                <button type="button" class="btn btn-primary" @click="modifyDept" id="dept_modify_submit">提交</button>
                                        </div>
                                </div>
                        </div>
                </div>


                <div class="modal fade" id="dept_drop_modal">
                        <div class="modal-dialog">
                                <div class="modal-content">
                                        <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title">删除确定</h4>
                                        </div>
                                        <div class="modal-body">
                                                <p>您真的要删除此部门吗？</p>
                                        </div>
                                        <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                                <button type="button" class="btn btn-primary" @click="deleteDept" id="dept_drop_submit">确认</button>
                                        </div>
                                </div>
                                <!-- /.modal-content -->
                        </div>
                        <!-- /.modal-dialog -->
                </div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                tabledata: ''
            }
        },
        methods: {
            getTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/dept/fetchallList',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    async: false,
                    error(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success(res) {
                        _this.tabledata = res.Table2
                    }
                });
                $('.table').bootstrapTable('destroy')
                $('.table').bootstrapTable({
                    columns: [{
                        radio: true
                    }, {
                        field: 'fidno',
                        title: '部门号',
                        // align: 'center',
                        halign: 'center',
                    }, {
                        field: 'fdname',
                        title: '部门名称',
                        halign: 'center',
                    }, {
                        field: 'entusername',
                        title: '创建人',
                        halign: 'center'
                    }],

                    data: this.tabledata,
                    striped: true,
                    pagination: true,
                    selectItemName: 'dept',
                    sidePagination: 'client',
                    pageNumber: 1,
                    pageSize: 5,
                    pageList: [10, 20, 50],
                    clickToSelect: true
                })
            },
            showAddModal() {
                $('#dept_add_modal').modal('show');
            },
            addDept() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/dept/add',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                        'fdid': $('#dept_add_parent').val(),
                        'fdname': $('#dept_add_name').val(),
                    },
                    async: true,
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        if (retdata.Table1[0].message == 'ok') {
                            $('#dept_add_modal').modal('hide');
                            _this.getTable()
                        }
                    }
                });
            },
            showModifyModal() {
                $('#dept_modify_modal').modal('show');
            },
            modifyDept() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/dept/update',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                        'fidno': $('.selected').children().eq(1).text(),
                        'fdname': $('#dept_modify_name').val(),
                        'fdid': $('#dept_modify_parent').val(),
                    },
                    async: true,
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#dept_modify_modal').modal('hide');
                        _this.getTable()
                    }
                });
            },
            showDeleteModal() {
                $('#dept_drop_modal').modal('show');
            },
            deleteDept() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/dept/delete',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'teamid': localStorage.activeteamid,
                        'fidno': $('.selected').children().eq(1).text(),
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#dept_drop_modal').modal('hide');
                        _this.getTable()
                    }
                });
            }
        },
        mounted() {
            this.getTable()
        }
    }
</script>
<style>
    .operation {
        float: left;
        margin: 10px;
    }
</style>